﻿using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Mvc.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CubicFWNet.Mvc.Controllers
{
    public class ModuleController : ControllerBase
    {
        private ModuleApp _moduleApp;

        public ModuleController()
        {
            _moduleApp = new ModuleApp();
        }
        [Authenticate]
        [HttpGet]
        public ViewResult Index()
        {
            ViewBag.Modules = _moduleApp.GetList();
            return View(_moduleApp.Load());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(Guid id)
        {
            var data = _moduleApp.GetForm(id);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {
            try
            {
                var del = _moduleApp.GetForm(id);
                if (del == null)
                {
                    Result.Status = "error";
                    Result.Message = "未找到该数据";
                    Result.ToJson();
                }
                _moduleApp.Delete(id);
            }
            catch (Exception e)
            {
                Result.Status = "error";
                Result.Message = e.Message;
            }
            return Result.ToJson();
        }

        //添加或修改角色
        [HttpPost]
        [HandlerAjaxOnly]
        public string Add(Module module)
        {
            try
            {
                module.CreatorId = operatorModel.UserId;
                _moduleApp.AddOrUpdate(module);
            }
            catch (Exception ex)
            {
                Result.Status = "error";
                Result.Message = ex.Message;
            }
            return Result.ToJson();
        }


    }
}